<template>

        <div id="reg">
            <el-form
                    label-width="100px"
                    :model="reg"
                    style="max-width: 460px"
            >
                <el-form-item label="手机号:">
                    <el-input v-model="reg.phone" />
                </el-form-item>

                <el-form-item label="验证码:">
                    <el-input style="width: 100px;display: inline-block;" v-model="reg.code" />
                    <el-button @click="sendCode()">发送验证码</el-button>
                </el-form-item>

                <el-form-item label="身份:">
                    <el-radio-group v-model="radio" size="large">
                        <el-radio-button label="求职者" value="1" />
                        <el-radio-button label="招聘者" value="2" />
                    </el-radio-group>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="login()">登录注册</el-button>

                </el-form-item>
            </el-form>
        </div>




</template>

<script setup>
import { reactive, ref } from 'vue'
import { FormProps } from 'element-plus'
import axios from "axios";
import router from "@/router";

const reg = ref({
  phone: "",
  code: "",
  roleId:""
})
const radio=ref("");
const sendCode=()=>{
    console.log(reg.value);

    axios.post('http://localhost:39081/users/sendCode',reg.value)
        .then((response)=>{
            if(response.data.state==20000){
                alert("验证码发送成功");
            }else {
                alert(response.data.msg);
            }
        })
};
const login=()=>{
if(radio.value=="求职者"){
    reg.value.roleId=1;
}else{
    reg.value.roleId=2;
}
    axios.post('http://localhost:39081/users/regAndLogin',reg.value)
        .then((response)=>{
            if(response.data.data.loginNumber==0){
                if (reg.value.roleId==1){

                    location.href="/addJobApplicat";
                }else {
                    location.href="/addRecruiter";
                }
            }else {
                location.href="/";
                // 让浏览器请求工程主地址,此时整个页面都会重新加载
            }
//把登录成功时的用户信息保存到LocalStorage里面
            localStorage.Authorization = JSON.stringify(response.data.data.token);
            localStorage.user=JSON.stringify(response.data.data.id);

            if(response.data.state==99999){
                alert(response.data.message);
            }
        })
}
</script>

<style scoped>


    #reg{
        width: 500px;
        height: 500px;
        margin: 200px  auto;
    }

</style>
